<template>
  <div class="tmpl">
    <h1>注册表单</h1>
    <el-form :model="registerForm" ref="registerForm" :rules="registerRules" label-width="80px" style="width:100%;max-width:500px;" status-icon >
      <el-form-item label="用户名" prop="username">
        <el-input placeholder="请输入用户名" v-model="registerForm.username" ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" required>
        <el-input placeholder="请输入密码" v-model="registerForm.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="所在区域">
        <el-select v-model="registerForm.region" placeholder="请选择所在区域">
          <el-option label="上海" value="上海"></el-option>
          <el-option label="北京" value="北京"></el-option>
          <el-option label="广州" value="广州"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="registerForm.sex">
          <el-radio-button label="男"></el-radio-button>
          <el-radio-button label="女"></el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动性质">
        <el-checkbox-group v-model="registerForm.type">
          <el-checkbox-button label="美食/餐厅线上活动" name="type"></el-checkbox-button>
          <el-checkbox-button label="地推活动" name="type"></el-checkbox-button>
          <el-checkbox-button label="线下主题活动" name="type"></el-checkbox-button>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="submitForm('registerForm')">提 交</el-button>
        <el-button type="danger" @click="resetForm('registerForm')">重 置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data () {
    var checkIsNull = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('字段不得为空'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        region: '',
        sex: '',
        type: []
      },
      registerRules: {
        username: [{ validator: checkIsNull, trigger: 'blur' }],
        password: [{ validator: checkIsNull, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let username = this.registerForm.username
          let password = this.registerForm.password
          let sex = this.registerForm.sex
          let region = this.registerForm.region
          let type = this.registerForm.type
          this.$message({
            showClose: true,
            message: `用户名：${username}, 密码：${password}, 性别：${sex}, 地区：${region}, 类型：${type}`,
            type: 'success'
          })
        } else {
          this.$message({
            showClose: true,
            message: '不允许登录',
            type: 'error'
          })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
h1 {
  font-weight: normal;
}
</style>
